package com.hwc.demo.dubbo.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.hwc.demo.dubbo.bean.UserAddress;
import com.hwc.demo.dubbo.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Component
@Service //com.alibaba.dubbo.config.annotation.Service
public class UserServiceImpl implements UserService{

	@HystrixCommand
	@Override
	public List<UserAddress> getUserAddressList(Integer userId) {
		System.out.println(">>in provider1 userId:" + userId);
		
		//模拟概率运行时异常
		if(Math.random()>0.5) {
			System.out.println(">> Random Exception Occur");
			throw new RuntimeException();
		}
				
		Map<Integer, List<UserAddress>> table = new HashMap<>();
		
		UserAddress address = new UserAddress("address01", 1);
		UserAddress address2 = new UserAddress("address02", 1);
		UserAddress address3 = new UserAddress("address04", 1);
		List<UserAddress> user1_list = Arrays.asList(address, address2, address3);
		
		table.put(1, user1_list);
		
		//超时测试
//		try {
//			Thread.sleep(3000);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}
		
		return table.get(userId);
	}

}
